/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.database;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import javax.annotation.Resource;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.log4j.Logger;

/**
 *
 * @author Insect
 */
public class SQLConnection {

    private InitialContext context;
    private Context env;
    private Logger myLogger = Logger.getLogger("RootLogger");
    @Resource(authenticationType = javax.annotation.Resource.AuthenticationType.CONTAINER, name = "jdbc/UserManager", type = javax.sql.DataSource.class)
    private DataSource ds;

    public Connection getMySQLConnection() {
        myLogger.info("Entering..");
        Connection connection = null;
        try {
            context = new InitialContext();
            env = (Context) context.lookup("java:comp/env");
            ds = (DataSource) env.lookup("jdbc/UserManager");
            connection = ds.getConnection();

        } catch (SQLException ex) {
            java.util.logging.Logger.getLogger(SQLConnection.class.getName()).log(Level.SEVERE, null, ex);
        } catch (NamingException ex) {
            java.util.logging.Logger.getLogger(SQLConnection.class.getName()).log(Level.SEVERE, null, ex);
        }
        myLogger.info("Exiting..");
        return connection;
    }
}
